package com.qdlc.p2p.biz.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.NewActivityPlanService;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dao.NewActivityPlanDao;
import com.qdlc.p2p.dal.dto.NewActivityPlan;
import com.qdlc.p2p.dal.dto.Operator;
import com.qdlc.p2p.dal.model.NewActivityPlanModel;

/**
 * Created by liaoxiangkai on 2015/11/12.
 */
@Service("newActivityPlanService")
public class NewActivityPlanServiceImpl implements NewActivityPlanService {

    @Resource
    private NewActivityPlanDao newActivityPlanDao;

    /**
     * 通过环节名称查询
     *
     * @param tache
     * @return
     */
    @Override
    public NewActivityPlan getActivityPlanByTache(String tache) {
        return newActivityPlanDao.getActivityPlanByTache(tache);
    }

    /**
     * 后台显示活动列表
     *
     * @param model
     * @return
     */
    @Override
    public PageDataList<NewActivityPlanModel> list(NewActivityPlanModel model) {
        return newActivityPlanDao.list(model);
    }

    /**
     * 切换活动开始关闭状态
     *
     * @param model
     * @return
     */
    @Override
    public int toggleStatus(NewActivityPlanModel model) {
        return newActivityPlanDao.toggleStatus(model);
    }

    /**
     * 通过id进行查询活动状态
     *
     * @param id
     * @return
     */
    @Override
    public NewActivityPlanModel find(long id) {
        NewActivityPlan entity = newActivityPlanDao.find(id);
        if (null == entity) return null;
        NewActivityPlanModel newActivityPlanModel = NewActivityPlanModel.instance(entity);
        //将修改者名字设置给model
        Operator modifier = entity.getModifier();
        if (null != modifier)
            newActivityPlanModel.setModifierName(modifier.getUserName());
        //把创建者名字设置给model
        Operator creator = entity.getCreator();
        if (null != creator)
            newActivityPlanModel.setCreatorName(entity.getCreator().getUserName());
        return newActivityPlanModel;
    }

    public NewActivityPlan save(NewActivityPlanModel model) {
        NewActivityPlan newActivityPlan = inverseModel(model);
        //保存新的记录设置创建者和创建时间
        newActivityPlan.setCreator(model.getCreator());
        newActivityPlan.setCreateTime(model.getCreateTime());
        return newActivityPlanDao.save(newActivityPlan);
    }

    /**
     * 更新活动
     *
     * @param model
     * @return
     */
    @Override
    public NewActivityPlan update(NewActivityPlanModel model) {
        NewActivityPlan newActivityPlan = inverseModel(model);
        newActivityPlan.setId(model.getId());
        //设置更新者更新时间
        newActivityPlan.setModifier(model.getModifier());
        newActivityPlan.setModifyTime(model.getModifyTime());
        Operator operator = newActivityPlanDao.find(model.getId()).getCreator();
        if (null == operator) {
            return null;
        }
        newActivityPlan.setCreator(operator);
        newActivityPlan.setCreateTime(model.getCreateTime());
        return newActivityPlanDao.update(newActivityPlan);
    }

    /**
     * -----------------------ajax请求增加一条新的活动规则--------------------
     * 查询所有活动方案
     *
     * @return
     */
    @Override
    public List<NewActivityPlan> findAll() {
        return newActivityPlanDao.findAll();
    }

    /**
     * 查询可以被添加的活动
     * @return
     */
    @Override
    public List<NewActivityPlan> isCanBeAdd(NewActivityPlanModel model) {
        return newActivityPlanDao.isCanBeAdd(model);
    }

    @Override
    public List<NewActivityPlan> findEnablePlan() {
        return newActivityPlanDao.findEnablePlan();
    }

    /**
     * 将model转成父类
     *
     * @param model
     * @return
     */
    public NewActivityPlan inverseModel(NewActivityPlanModel model) {
        NewActivityPlan newActivityPlan = new NewActivityPlan();
        newActivityPlan.setTache(model.getTache());
        newActivityPlan.setName(model.getName());
        newActivityPlan.setStatus(model.getStatus());
        newActivityPlan.setStartTime(model.getStartTime());
        newActivityPlan.setEndTime(model.getEndTime());
        newActivityPlan.setDescription(model.getDescription());
        return newActivityPlan;
    }
}
